<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 6.0.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/blog/images/logo.svg" color="#222">

<link rel="stylesheet" href="/blog/css/main.css">


<link rel="stylesheet" href="//cdn.bootcss.com/font-awesome/5.13.0/css/all.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"horsenliu.gitee.io","root":"/blog/","scheme":"Pisces","version":"7.8.0","exturl":false,"sidebar":{"position":"left","width":240,"display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":"default"},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":true,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="1. GEO 地理位置Redis 的 GEO 特性在 Redis 3.2 版本中推出， 这个功能可以将用户给定的地理位置信息储存起来， 并对这些信息进行操作。来实现诸如附近位置、摇一摇这类依赖于地理位置信息的功能。GEO 的数据类型为 Zset。 GEO 的数据结构总共有六个常用命令：GEOADD、GEOPOS、GEODIST、GEORADIUS、GEORADIUSBYMEMBER、GETHASH">
<meta property="og:type" content="article">
<meta property="og:title" content="04_三种特殊数据类型">
<meta property="og:url" content="http://horsenliu.gitee.io/blog/posts/154eaad9.html">
<meta property="og:site_name" content="霍森的笔记本">
<meta property="og:description" content="1. GEO 地理位置Redis 的 GEO 特性在 Redis 3.2 版本中推出， 这个功能可以将用户给定的地理位置信息储存起来， 并对这些信息进行操作。来实现诸如附近位置、摇一摇这类依赖于地理位置信息的功能。GEO 的数据类型为 Zset。 GEO 的数据结构总共有六个常用命令：GEOADD、GEOPOS、GEODIST、GEORADIUS、GEORADIUSBYMEMBER、GETHASH">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-12-01T08:10:00.000Z">
<meta property="article:modified_time" content="2021-07-28T12:58:45.182Z">
<meta property="article:author" content="Horsen Liu">
<meta property="article:tag" content="Redis">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://horsenliu.gitee.io/blog/posts/154eaad9.html">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>04_三种特殊数据类型 | 霍森的笔记本</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/blog/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">霍森的笔记本</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">记的不少 会的不多</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/blog/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/blog/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/blog/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签<span class="badge">123</span></a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/blog/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类<span class="badge">36</span></a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/blog/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档<span class="badge">220</span></a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>
  <a role="button" class="book-mark-link book-mark-link-fixed"></a>

  <a href="https://github.com/horsenliu" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://horsenliu.gitee.io/blog/posts/154eaad9.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/blog/images/avatar.png">
      <meta itemprop="name" content="Horsen Liu">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="霍森的笔记本">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          04_三种特殊数据类型
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2020-12-01 16:10:00" itemprop="dateCreated datePublished" datetime="2020-12-01T16:10:00+08:00">2020-12-01</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/%E5%B8%B8%E7%94%A8%E7%BB%84%E4%BB%B6%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/" itemprop="url" rel="index"><span itemprop="name">常用组件学习笔记</span></a>
                </span>
                  ，
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/%E5%B8%B8%E7%94%A8%E7%BB%84%E4%BB%B6%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/Redis/" itemprop="url" rel="index"><span itemprop="name">Redis</span></a>
                </span>
            </span>

          
            <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span id="busuanzi_value_page_pv"></span>
            </span><br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="far fa-file-word"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>4.5k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="far fa-clock"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>4 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h3 id="1-GEO-地理位置"><a href="#1-GEO-地理位置" class="headerlink" title="1. GEO 地理位置"></a>1. GEO 地理位置</h3><p>Redis 的 GEO 特性在 Redis 3.2 版本中推出， 这个功能可以将用户给定的地理位置信息储存起来， 并对<br>这些信息进行操作。来实现诸如附近位置、摇一摇这类依赖于地理位置信息的功能。GEO 的数据类型为 Zset。</p>
<p>GEO 的数据结构总共有六个常用命令：GEOADD、GEOPOS、GEODIST、GEORADIUS、GEORADIUSBYMEMBER、GETHASH。GEO 没有提供删除成员的命令，但是因为 GEO 的底层实现是 zset，所以可以<strong>借用 zrem 命令实现对地理位置信息的删除</strong>。</p>
<p>官方文档：<a target="_blank" rel="noopener" href="https://www.redis.net.cn/order/3685.html">https://www.redis.net.cn/order/3685.html</a></p>
<span id="more"></span>

<h4 id="1-1-GEOADD"><a href="#1-1-GEOADD" class="headerlink" title="1.1 GEOADD"></a>1.1 GEOADD</h4><p><code>GEOADD key longitude latitude member ...</code></p>
<p>将给定的空间元素（纬度、经度、名字）添加到指定的键里面。这些数据会以有序集合的形式被储存在键里面，从而使得 GEORADIUS 和GEORADIUSBYMEMBER 这样的命令可以在之后通过位置查询取得这些元素。</p>
<p>GEOADD 命令以标准的 x,y 格式接受参数，所以用户必须先输入经度，然后再输入纬度。</p>
<p>GEOADD 能够记录的坐标是有限的，非常接近两极的区域无法被索引。有效的经度介于-180-180 度之间，有效的纬度介于 -85.05112878 -85.05112878 度之间。当用户尝试输入一个超出范围的经度或者纬度时，GEOADD 命令将返回一个错误。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379&gt; GEOADD china:city 116.23 40.22 北京</span><br><span class="line">(<span class="built_in">integer</span>) 1</span><br><span class="line">127.0.0.1:6379&gt; GEOADD china:city 121.48 31.40 上海 113.88 22.55 深圳 120.21 30.20 杭州</span><br><span class="line">(<span class="built_in">integer</span>) 3</span><br><span class="line">127.0.0.1:6379&gt; GEOADD china:city 106.54 29.40 重庆 108.93 34.23 西安 114.02 30.58 武汉</span><br><span class="line">(<span class="built_in">integer</span>) 3</span><br></pre></td></tr></table></figure>

<h4 id="1-2-GEOPOS"><a href="#1-2-GEOPOS" class="headerlink" title="1.2 GEOPOS"></a>1.2 GEOPOS</h4><p><code>GEOPOS key member [member...]</code></p>
<p>从 key 里返回所有给定位置元素的位置（经度和纬度）</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379&gt; GEOPOS china:city 杭州 上海</span><br><span class="line">1) 1) <span class="string">&quot;120.21000176668167114&quot;</span></span><br><span class="line">   2) <span class="string">&quot;30.19999988833350102&quot;</span></span><br><span class="line">2) 1) <span class="string">&quot;121.48000091314315796&quot;</span></span><br><span class="line">   2) <span class="string">&quot;31.40000025319353938&quot;</span></span><br><span class="line">127.0.0.1:6379&gt; GEOPOS china:city 新疆</span><br><span class="line">1) (nil)</span><br></pre></td></tr></table></figure>

<h4 id="1-3-GEODIST"><a href="#1-3-GEODIST" class="headerlink" title="1.3 GEODIST"></a>1.3 GEODIST</h4><p><code>GEODIST key member1 member2 [unit]</code></p>
<p>返回两个给定位置之间的距离，如果两个位置之间的其中一个不存在，那么命令返回空值。</p>
<p>指定单位的参数 unit 必须是以下单位的其中一个：</p>
<ul>
<li>m 表示单位为米</li>
<li>km 表示单位为千米</li>
<li>mi 表示单位为英里</li>
<li>ft 表示单位为英尺</li>
<li>如果用户没有显式地指定单位参数，默认使用米作为单位</li>
</ul>
<p>GEODIST 命令在计算距离时会假设地球为完美的球形，在极限情况下，这一假设最大会造成 0.5% 的误差。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379&gt; GEODIST china:city 北京 上海</span><br><span class="line"><span class="string">&quot;1088785.4302&quot;</span></span><br><span class="line">127.0.0.1:6379&gt; GEODIST china:city 北京 上海 km</span><br><span class="line"><span class="string">&quot;1088.7854&quot;</span></span><br></pre></td></tr></table></figure>

<h4 id="1-4-GEORADIUS"><a href="#1-4-GEORADIUS" class="headerlink" title="1.4 GEORADIUS"></a>1.4 GEORADIUS</h4><p><code>GEORADIUS key longitude latitude radius m|km|ft|mi [withcoord][withdist][withhash][asc|desc][count count]</code></p>
<p>以给定的经纬度为中心， 找出某一半径内的元素。</p>
<p><em>重新连接 redis-cli，增加参数 –raw，可以强制输出中文，不然会出现乱码</em></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 在china:city中寻找坐标(100,30)半径为1000km的城市</span></span><br><span class="line">127.0.0.1:6379&gt; GEORADIUS china:city 100 30 1000 km</span><br><span class="line">重庆</span><br><span class="line">西安</span><br><span class="line"><span class="comment"># withdist 返回位置名称和中心距离</span></span><br><span class="line">127.0.0.1:6379&gt; GEORADIUS china:city 100 30 1000 km withdist</span><br><span class="line">重庆</span><br><span class="line">635.2850</span><br><span class="line">西安</span><br><span class="line">963.3171</span><br><span class="line"><span class="comment"># withcoord 返回位置名称和经纬度</span></span><br><span class="line">127.0.0.1:6379&gt; GEORADIUS china:city 100 30 1000 km withcoord</span><br><span class="line">重庆</span><br><span class="line">106.54000014066696167</span><br><span class="line">29.39999880018641676</span><br><span class="line">西安</span><br><span class="line">108.92999857664108276</span><br><span class="line">34.23000121926852302</span><br><span class="line"><span class="comment"># count 限定寻找个数</span></span><br><span class="line">127.0.0.1:6379&gt; GEORADIUS china:city 100 30 1000 km withcoord count 1</span><br><span class="line">重庆</span><br><span class="line">106.54000014066696167</span><br><span class="line">29.39999880018641676</span><br></pre></td></tr></table></figure>

<h4 id="1-5-GEORADIUSBYMAMBER"><a href="#1-5-GEORADIUSBYMAMBER" class="headerlink" title="1.5 GEORADIUSBYMAMBER"></a>1.5 GEORADIUSBYMAMBER</h4><p><code>GEORADIUSBYMAMBER key member radius m|km|ft|mi [withcoord][withdist][withhash][asc|desc][count count]</code></p>
<p>找出位于指定范围内的元素，中心点由给定的位置元素决定。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379&gt; GEORADIUSBYMEMBER china:city 北京 1000 km</span><br><span class="line">北京</span><br><span class="line">西安</span><br><span class="line">127.0.0.1:6379&gt; GEORADIUSBYMEMBER china:city 上海 400 km</span><br><span class="line">杭州</span><br><span class="line">上海</span><br></pre></td></tr></table></figure>

<h4 id="1-6-GEOHASH"><a href="#1-6-GEOHASH" class="headerlink" title="1.6 GEOHASH"></a>1.6 GEOHASH</h4><p><code>GEOHASH key member [member...]</code></p>
<p>Redis 使用 GEOHASH 将二维经纬度转换为一维字符串，字符串越长表示位置更精确，两个字符串越相似表示距离越近。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379&gt; GEOHASH china:city 北京 重庆</span><br><span class="line">wx4sucu47r0</span><br><span class="line">wm5z22h53v0</span><br><span class="line">127.0.0.1:6379&gt; GEOHASH china:city 上海 北京</span><br><span class="line">wtw6sk5n300</span><br><span class="line">wx4sucu47r0</span><br></pre></td></tr></table></figure>

<h3 id="2-HyperLogLog"><a href="#2-HyperLogLog" class="headerlink" title="2. HyperLogLog"></a>2. HyperLogLog</h3><h4 id="2-1-简介"><a href="#2-1-简介" class="headerlink" title="2.1 简介"></a>2.1 简介</h4><p>Redis 在 2.8.9 版本添加了 HyperLogLog 结构。</p>
<p>Redis HyperLogLog 是用来做基数统计的算法，HyperLogLog 的优点是，在输入元素的数量或者体积非常非常大时，计算基数所需的空间总是固定 的、并且是很小的。</p>
<p>在 Redis 里面，每个 HyperLogLog 键<strong>只需要花费 12 KB 内存</strong>，就可以计算接近 2^64 个不同元素的<strong>基数</strong>。这和计算基数时，元素越多耗费内存就越多的集合形成鲜明对比。</p>
<p>HyperLogLog 则是一种算法，它提供了不精确的去重计数方案。</p>
<p>举个栗子：假如我要统计网页的 UV（浏览用户数量，一天内同一个用户多次访问只能算一次），传统的解决方案是使用 Set 来保存用户 id，然后统计 Set 中的元素数量来获取页面 UV。但这种方案只能承载少量用户，一旦用户数量大起来就需要消耗大量的空间来存储用户 id。我的目的是统计用户数量而不是保存用户，这简直是个吃力不讨好的方案，而使用 Redis 的 HyperLogLog 最多需要 12k 就可以统计大量的用户数，尽管它大概有 0.81% 的错误率，但对于统计 UV 这种不需要很精确的数据是可以忽略不计的。</p>
<h4 id="2-2-什么是基数"><a href="#2-2-什么是基数" class="headerlink" title="2.2 什么是基数"></a>2.2 什么是基数</h4><p>简单来说就是数据集中不重复的元素的个数。</p>
<p>例如数据集 A&#x3D;{1, 3, 5, 7, 5, 7, 8}，那么这个数据集的基数集为 {1, 3, 5 ,7 ,8}，基数为 5。</p>
<h4 id="2-3-基本命令"><a href="#2-3-基本命令" class="headerlink" title="2.3 基本命令"></a>2.3 基本命令</h4><table>
<thead>
<tr>
<th>命令</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td><code>PFADD key element [element ...]</code></td>
<td>添加指定元素到 HyperLogLog 中</td>
</tr>
<tr>
<td><code>PFCOUNT key [key ...]</code></td>
<td>返回给定 HyperLogLog 的基数估算值</td>
</tr>
<tr>
<td><code>PFMERGE destkey sourcekey [sourcekey ...]</code></td>
<td>将多个 HyperLogLog 合并为一个 HyperLogLog 并计算</td>
</tr>
</tbody></table>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379&gt; PFADD key a b c d e f</span><br><span class="line">1</span><br><span class="line">127.0.0.1:6379&gt; PFCOUNT key</span><br><span class="line">6</span><br><span class="line">127.0.0.1:6379&gt; PFADD key2 g h i j a b c</span><br><span class="line">1</span><br><span class="line">127.0.0.1:6379&gt; PFMERGE key3 key key2</span><br><span class="line">OK</span><br><span class="line">127.0.0.1:6379&gt; PFCOUNT key3</span><br><span class="line">10</span><br></pre></td></tr></table></figure>

<h3 id="3-BitMaps"><a href="#3-BitMaps" class="headerlink" title="3. BitMaps"></a>3. BitMaps</h3><p>在开发中，可能会遇到这种情况，需要统计用户的某些信息，如活跃或不活跃，登录或者不登录；需要记录用户一年的打卡情况，打卡了是 1， 没有打卡是 0，如果使用普通的 key&#x2F;value 存储，则要记录 365 条记录，如果用户量很大，需要的空间也会很大，所以 Redis 提供了 BitMaps 位图这中数据结构。</p>
<p>BitMaps 就是通过操作二进制位来进行记录，即 0 和 1；如果要记录 365 天的打卡情况，使用 BitMaps 表示的形式大概如下：0101000111000111…，好处是可以节约内存，365 天相当于 365 bit，1 字节 &#x3D; 8 bit，所以使用 46 个字节即可。</p>
<p>BitMaps  就是通过一个 bit 位来表示某个元素对应的值或者状态，其中的 key 就是对应元素本身，实际上底层也是通过对字符串的操作来实现。</p>
<hr>
<h4 id="3-1-SETBIT"><a href="#3-1-SETBIT" class="headerlink" title="3.1 SETBIT"></a>3.1 SETBIT</h4><p><code>SETBIT key offset value</code> 设置 key 的第 offset 位为 value (1或0)</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 周一：1，周二：0，周三：0，周四：1，周五：1，周六：0，周天：0 （1 为打卡，0 为不打卡）</span></span><br><span class="line">127.0.0.1:6379&gt; SETBIT sign 0 1</span><br><span class="line">0</span><br><span class="line">127.0.0.1:6379&gt; SETBIT sign 1 0</span><br><span class="line">0</span><br><span class="line">127.0.0.1:6379&gt; SETBIT sign 2 0</span><br><span class="line">0</span><br><span class="line">127.0.0.1:6379&gt; SETBIT sign 3 1</span><br><span class="line">0</span><br><span class="line">127.0.0.1:6379&gt; SETBIT sign 4 1</span><br><span class="line">0</span><br><span class="line">127.0.0.1:6379&gt; SETBIT sign 5 0</span><br><span class="line">0</span><br><span class="line">127.0.0.1:6379&gt; SETBIT sign 6 0</span><br><span class="line">0</span><br></pre></td></tr></table></figure>

<h4 id="3-2-GETBIT"><a href="#3-2-GETBIT" class="headerlink" title="3.2 GETBIT"></a>3.2 GETBIT</h4><p><code>GETBIT key offset</code> 获取 offset 设置的值，未设置过默认返回 0</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379&gt; GETBIT sign 3</span><br><span class="line">1</span><br><span class="line">127.0.0.1:6379&gt; GETBIT sign 4</span><br><span class="line">1</span><br><span class="line">127.0.0.1:6379&gt; GETBIT sign 6</span><br><span class="line">0</span><br></pre></td></tr></table></figure>

<h4 id="3-3-BITCOUNT"><a href="#3-3-BITCOUNT" class="headerlink" title="3.3 BITCOUNT"></a>3.3 BITCOUNT</h4><p><code>BITCOUNT key [start, end] </code>统计 key 中 offset 为1 的个数</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379&gt; BITCOUNT sign</span><br><span class="line">3</span><br></pre></td></tr></table></figure>


    </div>

    
    
    
        

<div>
<ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>Horsen Liu
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="http://horsenliu.gitee.io/blog/posts/154eaad9.html" title="04_三种特殊数据类型">http://horsenliu.gitee.io/blog/posts/154eaad9.html</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>


      <footer class="post-footer">
          
          <div class="post-tags">
              <a href="/blog/tags/Redis/" rel="tag"><i class="fa fa-tag"></i> Redis</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/blog/posts/8fb13bff.html" rel="prev" title="03_五大数据类型">
      <i class="fa fa-chevron-left"></i> 03_五大数据类型
    </a></div>
      <div class="post-nav-item">
    <a href="/blog/posts/cf821b7.html" rel="next" title="05_Redis 事务">
      05_Redis 事务 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-GEO-%E5%9C%B0%E7%90%86%E4%BD%8D%E7%BD%AE"><span class="nav-text">1. GEO 地理位置</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1-1-GEOADD"><span class="nav-text">1.1 GEOADD</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-2-GEOPOS"><span class="nav-text">1.2 GEOPOS</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-3-GEODIST"><span class="nav-text">1.3 GEODIST</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-4-GEORADIUS"><span class="nav-text">1.4 GEORADIUS</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-5-GEORADIUSBYMAMBER"><span class="nav-text">1.5 GEORADIUSBYMAMBER</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-6-GEOHASH"><span class="nav-text">1.6 GEOHASH</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-HyperLogLog"><span class="nav-text">2. HyperLogLog</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#2-1-%E7%AE%80%E4%BB%8B"><span class="nav-text">2.1 简介</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-2-%E4%BB%80%E4%B9%88%E6%98%AF%E5%9F%BA%E6%95%B0"><span class="nav-text">2.2 什么是基数</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-3-%E5%9F%BA%E6%9C%AC%E5%91%BD%E4%BB%A4"><span class="nav-text">2.3 基本命令</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-BitMaps"><span class="nav-text">3. BitMaps</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#3-1-SETBIT"><span class="nav-text">3.1 SETBIT</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-2-GETBIT"><span class="nav-text">3.2 GETBIT</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-3-BITCOUNT"><span class="nav-text">3.3 BITCOUNT</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Horsen Liu"
      src="/blog/images/avatar.png">
  <p class="site-author-name" itemprop="name">Horsen Liu</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/blog/archives/">
        
          <span class="site-state-item-count">220</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/blog/categories/">
          
        <span class="site-state-item-count">36</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/blog/tags/">
          
        <span class="site-state-item-count">123</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/horsenliu" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;horsenliu" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i></a>
      </span>
      <span class="links-of-author-item">
        <a href="https://wpa.qq.com/msgrd?v=3&uin=1468224254&site=qq&menu=yes" title="QQ → https:&#x2F;&#x2F;wpa.qq.com&#x2F;msgrd?v&#x3D;3&amp;uin&#x3D;1468224254&amp;site&#x3D;qq&amp;menu&#x3D;yes" rel="noopener" target="_blank"><i class="fab fa-qq fa-fw"></i></a>
      </span>
      <span class="links-of-author-item">
        <a href="https://sm.ms/image/jyArGeatQ9Plw4n" title="WeChat → https:&#x2F;&#x2F;sm.ms&#x2F;image&#x2F;jyArGeatQ9Plw4n" rel="noopener" target="_blank"><i class="fab fa-weixin fa-fw"></i></a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:horsenliu@qq.com" title="E-Mail → mailto:horsenliu@qq.com" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i></a>
      </span>
  </div>
  <div class="cc-license motion-element" itemprop="license">
    <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" class="cc-opacity" rel="noopener" target="_blank"><img src="/blog/images/cc-by-nc-sa.svg" alt="Creative Commons"></a>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Horsen Liu</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-area"></i>
    </span>
      <span class="post-meta-item-text">站点总字数：</span>
    <span title="站点总字数">707k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span class="post-meta-item-text">站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">10:43</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://pisces.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Pisces</a> 强力驱动
  </div><script color="252,100,35" pointColor="252,100,35" opacity="1" zIndex="-1" count="150" src="https://cdn.jsdelivr.net/npm/canvas-nest.js@1/dist/canvas-nest.js"></script>

        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/blog/lib/anime.min.js"></script>
  <script src="/blog/lib/velocity/velocity.min.js"></script>
  <script src="/blog/lib/velocity/velocity.ui.min.js"></script>

<script src="/blog/js/utils.js"></script>

<script src="/blog/js/motion.js"></script>


<script src="/blog/js/schemes/pisces.js"></script>


<script src="/blog/js/next-boot.js"></script>

<script src="/blog/js/bookmark.js"></script>




  




  
<script src="/blog/js/local-search.js"></script>













  

  

<script src="/blog/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"debug":false,"model":{"scale":1,"hHeadPos":0.5,"vHeadPos":0.618,"jsonPath":"/blog/live2dw/assets/assets/hijiki.model.json"},"display":{"superSample":2,"width":180,"height":360,"position":"left","hOffset":40,"vOffset":-60},"mobile":{"show":false,"scale":0.1},"react":{"opacityDefault":0.7,"opacityOnHover":0.8},"log":false});</script></body>
</html>
